All files / controllers LandingPageController.js

0% Statements 0/178
0% Branches 0/100
0% Functions 0/19
0% Lines 0/177

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
/**
 * Landing Page Controller
 * Manages landing page settings and content
 */
 
const BaseController = require('./BaseController');
const { pool } = require('../config/database');
const { logger } = require('../config/logger');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
 
// Configure multer for landing page logo uploads
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    const uploadDir = path.join(__dirname, '../public/uploads/landing');
    if (!fs.existsSync(uploadDir)) {
      fs.mkdirSync(uploadDir, { recursive: true });
    }
    cb(null, uploadDir);
  },
  filename: function (req, file, cb) {
    const ext = path.extname(file.originalname);
    const fieldName = file.fieldname.replace('_file', '');
    cb(null, `${fieldName}-${Date.now()}${ext}`);
  }
});
 
const uploadLogos = multer({
  storage: storage,
  limits: { fileSize: 2 * 1024 * 1024 }, // 2MB
  fileFilter: function (req, file, cb) {
    const allowedTypes = /jpeg|jpg|png|gif|svg|webp/;
    const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
    const mimetype = allowedTypes.test(file.mimetype) || file.mimetype === 'image/svg+xml';
    if (extname || mimetype) {
      return cb(null, true);
    }
    cb(new Error('Only image files are allowed (jpg, png, gif, svg, webp)'));
  }
}).fields([
  { name: 'company_logo_file', maxCount: 1 },
  { name: 'header_logo_file', maxCount: 1 },
  { name: 'hero_logo_file', maxCount: 1 },
  { name: 'footer_logo_file', maxCount: 1 }
]);
 
class LandingPageController extends BaseController {
  /**
   * Get landing page settings (PUBLIC)
   */
  static async getSettings(req, res) {
    try {
      const [settings] = await pool.execute(
        'SELECT * FROM landing_page_settings WHERE id = 1'
      );
 
      const [features] = await pool.execute(
        'SELECT * FROM landing_page_features WHERE active = 1 ORDER BY sort_order'
      );
 
      const [testimonials] = await pool.execute(
        'SELECT * FROM landing_page_testimonials WHERE active = 1 ORDER BY sort_order'
      );
 
      // Get active subscription plans
      const [plans] = await pool.execute(
        `SELECT 
           sp.*, 
           COALESCE(c.symbol, dc.symbol) as currency_symbol,
           COALESCE(sp.currency, dc.code) as currency
         FROM subscription_plans sp
         LEFT JOIN currencies c ON c.code = sp.currency
         LEFT JOIN (
           SELECT code, symbol 
           FROM currencies 
           WHERE is_default = TRUE AND active = TRUE 
           ORDER BY id 
           LIMIT 1
         ) dc ON 1 = 1
         WHERE sp.active = 1
         ORDER BY sp.sort_order, sp.price`
      );
 
      // Format plans for display
      const formattedPlans = (plans || []).map(plan => {
        const planFeatures = [];
 
        // Add resource limits
        if (plan.max_messages_per_month) {
          planFeatures.push({
            type: 'limit',
            text: `${plan.max_messages_per_month.toLocaleString()} messages/month`,
            enabled: true
          });
        }
        if (plan.max_users) {
          planFeatures.push({
            type: 'limit',
            text: `${plan.max_users} ${plan.max_users === 1 ? 'user' : 'users'}`,
            enabled: true
          });
        }
        if (plan.max_conversations) {
          planFeatures.push({
            type: 'limit',
            text: `${plan.max_conversations.toLocaleString()} conversations`,
            enabled: true
          });
        }
 
        // Add feature toggles
        if (plan.whatsapp_enabled) {
          planFeatures.push({
            type: 'feature',
            text: 'WhatsApp Integration',
            enabled: true
          });
        }
        if (plan.ai_enabled) {
          planFeatures.push({
            type: 'feature',
            text: 'AI-Powered Responses',
            enabled: true
          });
        }
        if (plan.analytics_enabled) {
          planFeatures.push({
            type: 'feature',
            text: 'Analytics & Reports',
            enabled: true
          });
        }
        if (plan.priority_support_enabled) {
          planFeatures.push({
            type: 'feature',
            text: 'Priority Support',
            enabled: true
          });
        }
        if (plan.api_access_enabled) {
          planFeatures.push({
            type: 'feature',
            text: 'API Access',
            enabled: true
          });
        }
        if (plan.custom_branding_enabled) {
          planFeatures.push({
            type: 'feature',
            text: 'Custom Branding',
            enabled: true
          });
        }
 
        return {
          ...plan,
          formatted_features: planFeatures
        };
      });
 
      return res.json({
        success: true,
        data: {
          settings: settings[0] || {},
          features: features || [],
          testimonials: testimonials || [],
          plans: formattedPlans || []
        }
      });
    } catch (error) {
      logger.error('Error getting landing page settings', { error: error.message, stack: error.stack });
      return res.status(500).json({
        success: false,
        message: 'Error loading landing page: ' + error.message
      });
    }
  }
 
  /**
   * Update landing page settings (SUPER ADMIN)
   */
  static async updateSettings(req, res) {
    try {
      const settings = req.body;
 
      const updateFields = [];
      const values = [];
 
      // Build dynamic update query
      Object.keys(settings).forEach(key => {
        if (key !== 'id') {
          updateFields.push(`${key} = ?`);
          values.push(settings[key]);
        }
      });
 
      if (updateFields.length === 0) {
        return res.status(400).json({
          success: false,
          message: 'No fields to update'
        });
      }
 
      const query = `UPDATE landing_page_settings SET ${updateFields.join(', ')} WHERE id = 1`;
      await pool.execute(query, values);
 
      logger.info('Landing page settings updated');
 
      return res.json({
        success: true,
        message: 'Settings updated successfully'
      });
    } catch (error) {
      logger.error('Error updating landing page settings', { error: error.message });
      return res.status(500).json({
        success: false,
        message: 'Error updating settings'
      });
    }
  }
 
  /**
   * Get features
   */
  static async getFeatures(req, res) {
    try {
      const [features] = await pool.execute(
        'SELECT * FROM landing_page_features ORDER BY sort_order'
      );
 
      return res.json({
        success: true,
        data: features
      });
    } catch (error) {
      logger.error('Error getting features', { error: error.message });
      return res.status(500).json({
        success: false,
        message: 'Error loading features'
      });
    }
  }
 
  /**
   * Create feature
   */
  static async createFeature(req, res) {
    try {
      const { icon, title, description, sort_order, active } = req.body;
 
      const [result] = await pool.execute(
        'INSERT INTO landing_page_features (icon, title, description, sort_order, active) VALUES (?, ?, ?, ?, ?)',
        [icon, title, description, sort_order || 0, active !== undefined ? active : 1]
      );
 
      return res.status(201).json({
        success: true,
        message: 'Feature created successfully',
        data: { id: result.insertId }
      });
    } catch (error) {
      logger.error('Error creating feature', { error: error.message });
      return res.status(500).json({
        success: false,
        message: 'Error creating feature'
      });
    }
  }
 
  /**
   * Update feature
   */
  static async updateFeature(req, res) {
    try {
      const { id } = req.params;
      const { icon, title, description, sort_order, active } = req.body;
 
      // Get current feature data first
      const [existing] = await pool.execute(
        'SELECT * FROM landing_page_features WHERE id = ?',
        [id]
      );
 
      if (existing.length === 0) {
        return res.status(404).json({
          success: false,
          message: 'Feature not found'
        });
      }
 
      const currentFeature = existing[0];
 
      // Use existing values if not provided (partial update support)
      const updatedIcon = icon !== undefined ? icon : currentFeature.icon;
      const updatedTitle = title !== undefined ? title : currentFeature.title;
      const updatedDescription = description !== undefined ? description : currentFeature.description;
      const updatedSortOrder = sort_order !== undefined ? sort_order : currentFeature.sort_order;
      const updatedActive = active !== undefined ? active : currentFeature.active;
 
      await pool.execute(
        'UPDATE landing_page_features SET icon = ?, title = ?, description = ?, sort_order = ?, active = ? WHERE id = ?',
        [updatedIcon, updatedTitle, updatedDescription, updatedSortOrder, updatedActive, id]
      );
 
      return res.json({
        success: true,
        message: 'Feature updated successfully'
      });
    } catch (error) {
      logger.error('Error updating feature', { error: error.message, stack: error.stack });
      return res.status(500).json({
        success: false,
        message: 'Error updating feature'
      });
    }
  }
 
  /**
   * Delete feature
   */
  static async deleteFeature(req, res) {
    try {
      const { id } = req.params;
 
      await pool.execute('DELETE FROM landing_page_features WHERE id = ?', [id]);
 
      return res.json({
        success: true,
        message: 'Feature deleted successfully'
      });
    } catch (error) {
      logger.error('Error deleting feature', { error: error.message });
      return res.status(500).json({
        success: false,
        message: 'Error deleting feature'
      });
    }
  }
 
  /**
   * Get testimonials
   */
  static async getTestimonials(req, res) {
    try {
      const [testimonials] = await pool.execute(
        'SELECT * FROM landing_page_testimonials ORDER BY sort_order'
      );
 
      return res.json({
        success: true,
        data: testimonials
      });
    } catch (error) {
      logger.error('Error getting testimonials', { error: error.message });
      return res.status(500).json({
        success: false,
        message: 'Error loading testimonials'
      });
    }
  }
 
  /**
   * Create testimonial
   */
  static async createTestimonial(req, res) {
    try {
      const { customer_name, customer_title, customer_company, customer_avatar, testimonial_text, rating, sort_order, active } = req.body;
 
      const [result] = await pool.execute(
        'INSERT INTO landing_page_testimonials (customer_name, customer_title, customer_company, customer_avatar, testimonial_text, rating, sort_order, active) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
        [customer_name, customer_title, customer_company, customer_avatar, testimonial_text, rating || 5, sort_order || 0, active !== undefined ? active : 1]
      );
 
      return res.status(201).json({
        success: true,
        message: 'Testimonial created successfully',
        data: { id: result.insertId }
      });
    } catch (error) {
      logger.error('Error creating testimonial', { error: error.message });
      return res.status(500).json({
        success: false,
        message: 'Error creating testimonial'
      });
    }
  }
 
  /**
   * Update testimonial
   */
  static async updateTestimonial(req, res) {
    try {
      const { id } = req.params;
      const { customer_name, customer_title, customer_company, customer_avatar, testimonial_text, rating, sort_order, active } = req.body;
 
      await pool.execute(
        'UPDATE landing_page_testimonials SET customer_name = ?, customer_title = ?, customer_company = ?, customer_avatar = ?, testimonial_text = ?, rating = ?, sort_order = ?, active = ? WHERE id = ?',
        [customer_name, customer_title, customer_company, customer_avatar, testimonial_text, rating, sort_order, active, id]
      );
 
      return res.json({
        success: true,
        message: 'Testimonial updated successfully'
      });
    } catch (error) {
      logger.error('Error updating testimonial', { error: error.message });
      return res.status(500).json({
        success: false,
        message: 'Error updating testimonial'
      });
    }
  }
 
  /**
   * Delete testimonial
   */
  static async deleteTestimonial(req, res) {
    try {
      const { id } = req.params;
 
      await pool.execute('DELETE FROM landing_page_testimonials WHERE id = ?', [id]);
 
      return res.json({
        success: true,
        message: 'Testimonial deleted successfully'
      });
    } catch (error) {
      logger.error('Error deleting testimonial', { error: error.message });
      return res.status(500).json({
        success: false,
        message: 'Error deleting testimonial'
      });
    }
  }
 
  /**
   * Generate WhatsApp link
   */
  static generateWhatsAppLink(req, res) {
    try {
      const { phone, message } = req.body;
 
      if (!phone) {
        return res.status(400).json({
          success: false,
          message: 'Phone number is required'
        });
      }
 
      // Remove non-numeric characters
      const cleanPhone = phone.replace(/\D/g, '');
 
      // Generate link
      let link = `https://wa.me/${cleanPhone}`;
      
      if (message) {
        link += `?text=${encodeURIComponent(message)}`;
      }
 
      return res.json({
        success: true,
        data: {
          link,
          phone: cleanPhone,
          message: message || ''
        }
      });
    } catch (error) {
      logger.error('Error generating WhatsApp link', { error: error.message });
      return res.status(500).json({
        success: false,
        message: 'Error generating link'
      });
    }
  }
 
  /**
   * Upload landing page logos
   * POST /api/landing/upload-logos
   */
  static uploadLogos(req, res) {
    uploadLogos(req, res, async function(err) {
      if (err) {
        logger.error('Logo upload error', { error: err.message });
        return res.status(400).json({
          success: false,
          message: err.message
        });
      }
 
      try {
        const logoFields = ['company_logo', 'header_logo', 'hero_logo', 'footer_logo'];
        const updates = {};
 
        for (const field of logoFields) {
          const fileField = `${field}_file`;
          
          // Handle file upload
          if (req.files && req.files[fileField]) {
            const file = req.files[fileField][0];
            const logoUrl = `/uploads/landing/${file.filename}`;
            
            // Delete old logo if exists
            const [oldSettings] = await pool.execute(
              `SELECT ${field} FROM landing_page_settings WHERE id = 1`
            );
            if (oldSettings[0] && oldSettings[0][field]) {
              const oldPath = path.join(__dirname, '../public', oldSettings[0][field]);
              if (fs.existsSync(oldPath) && oldSettings[0][field].includes('/uploads/landing/')) {
                try {
                  fs.unlinkSync(oldPath);
                } catch (e) {
                  logger.warn('Could not delete old logo', { path: oldPath });
                }
              }
            }
            
            updates[field] = logoUrl;
          }
          
          // Handle removal request
          const removeField = `remove_${field}`;
          if (req.body[removeField] === 'true') {
            const [oldSettings] = await pool.execute(
              `SELECT ${field} FROM landing_page_settings WHERE id = 1`
            );
            if (oldSettings[0] && oldSettings[0][field]) {
              const oldPath = path.join(__dirname, '../public', oldSettings[0][field]);
              if (fs.existsSync(oldPath) && oldSettings[0][field].includes('/uploads/landing/')) {
                try {
                  fs.unlinkSync(oldPath);
                } catch (e) {
                  logger.warn('Could not delete logo', { path: oldPath });
                }
              }
            }
            updates[field] = null;
          }
        }
 
        // Update database
        if (Object.keys(updates).length > 0) {
          const updateFields = Object.keys(updates).map(k => `${k} = ?`).join(', ');
          const values = Object.values(updates);
          
          await pool.execute(
            `UPDATE landing_page_settings SET ${updateFields} WHERE id = 1`,
            values
          );
          
          // Also update system_settings_kv if company_logo changed
          if (updates.company_logo !== undefined) {
            if (updates.company_logo) {
              await pool.execute(
                `INSERT INTO system_settings_kv (setting_key, setting_value) 
                 VALUES ('system_logo', ?) 
                 ON DUPLICATE KEY UPDATE setting_value = ?`,
                [updates.company_logo, updates.company_logo]
              );
            } else {
              await pool.execute(
                "DELETE FROM system_settings_kv WHERE setting_key = 'system_logo'"
              );
            }
          }
        }
 
        logger.info('Landing page logos updated');
 
        return res.json({
          success: true,
          message: 'Logos updated successfully',
          data: updates
        });
      } catch (error) {
        logger.error('Error uploading logos', { error: error.message });
        return res.status(500).json({
          success: false,
          message: 'Error uploading logos'
        });
      }
    });
  }
}
 
module.exports = LandingPageController;